<?php

	class userAction extends Action {
		
		protected $userInfo = null; //用户信息
		protected $rank = false;
		protected  function _initialize(){ //初始化action,用于判断用户登录与否？
			$this->userInfo = Session::get("userInfo");
			if(empty($this->userInfo)){
				$this->redirect("login","login");
			}
			$this->assign("userInfo",$this->userInfo);
			//私信
			$message = D("message")->count("userid=".$this->userInfo['id']." and isread=0","id");
			if($message>0){
				$this->assign("messagecount",$message);
			}
		}
		
		
		/**
		  * 个人空间
		  *
		  *
		  **/
		public function index(){ //默认action
			isset($_GET["u"]) ? $userId = safeRequest($_GET["u"]) : $userId = $this->userInfo['id'];
			$this->rank =  getRank($this->userInfo['id'],$userId,"home");
			if(!$this->rank){
				redirect(__APP__."/rank/norank/u/".$userId);
			}		
			$guestList  =  D("guestview")->findAll("guest.userid=".$userId." and guest.guestid=user.id","","regtime desc",10); 	//最新10条留言
			$miniList   =  D("mini")->findAll("userid=".$userId,"*","id desc",10);    	//读取最新迷你博客
			$feedList   =  D("feed")->findAll("userid=".$userId,"*","regtime desc",5);		//读取最新动态
			$userHEdu	=  D("user_edu")->findAll("userid=".$userId." and school_type=1","*","regtime desc");	//用户大学信息
			$userMEdu	=  D("user_edu")->findAll("userid=".$userId." and school_type=2","school_name,regtime","regtime desc ");	//用户中学信息
			$userWork	=  D("user_work")->findAll("userid=".$userId,"*");						//用户工作信息
			$userContact=  D("user_contact")->find("userid=".$userId);					//用户联系信息
			$userIntro	=  D("user_info")->find("userid=".$userId);						//用户爱好信息
			
			$path = getCachePath(APP_PATH."/UserCache",$this->userInfo['id']);			//用户cache目录
			$userInfo   =  F("userInfo","",-1,$path);//读取用户信息cache
			//$userInfo = D("user")->find("id=".$userId);
			$this->assign("userInfo",$userInfo);
			$userData   =  F("userData","",-1,$path);   									//读取用户数据cache
			//$userData = D("cache")->find("userid=".$userId);			
			//$friendList =  $userData->friendCount >0  ? F("friendList","",$path) : "";	//读取最新好友cache	
			$friendList =  $userData['friendCount'] >0 ? D("friendview")->findAll("friends.userid=".$userId." and friends.friendid=user.id","","lasttime desc",6) : "";	//读取最新好友cache
			//$blogList   =  $userData->friendCount >0  ? F("blogList","",$path)   : "";	//读取最新博客cache
			$blogList   =  $userData['blogCount'] >0  ? D("blogs")->findAll("userid=".$userId." and showtype=1","id,regtime,title,revertnum","regtime desc",10) : "";
			//$shareList  =  $userData['shareCount']  >0  ? F("shareList","",$path)  : "";	//读取最新共享cache
			$shareList  =   $userData['shareCount'] >0  ? D("shars")->findAll("userid=".$userId,"*","regtime desc",5): "";	//读取最新共享cache
			//$groupList  =  $userData['groupCount']  >0  ? F("groupList","",$path)  : "";  //读取最新群组cache
			$groupList  =  $userData['groupCount']  >0  ? D("groups_users")->findAll("userid=".$userId,"groupid","usertype desc",30)  : "";  //读取最新群组
			//$activeList =  $userData['activeCount'] >0  ? F("activeList","",$path) : ""; 	//读取最新活动cache
			$activeList =  $userData['activeCount'] >0  ? D("active_user")->findAll("userid=".$userId,"groupid","regtime desc",30) : ""; 	//读取最新活动cache
			//$albumList  =  $userData['albumCount']  >0  ? F("albumList","",$path)  : "";  //读取最新相册cache
			$albumList  =  $userData['albumCount']  >0  ? D("albums")->findAll("userid=".$userId,"*","modifytime desc",2)  : "";  //读取最新相册cache
			
			if($this->userInfo['id'] != $userId){
				$isFriend = isFriend($userId,$this->userInfo['id']);
				if("yes" == $isFriend){
					D("friends")->setField("lasttime",time(),"userid=".$userId." and friendid=".$this->userInfo['id']);
				}
				$this->assign("isFriend",$isFriend);
			}
			$this->assign("myId",$this->userInfo['id']);
			$this->assign("miniList",$miniList);
			$this->assign("friendList",$friendList);
			$this->assign("blogList",$blogList);
			$this->assign("shareList",$shareList);
			$this->assign("groupList",$groupList);
			$this->assign("guestList",$guestList);
			$this->assign("feedList",$feedList);
			$this->assign("activeList",$activeList);
			$this->assign("albumList",$albumList);
			$this->assign("userData",$userData);
			$this->assign("userHEdu",$userHEdu);
			$this->assign("userMEdu",$userMEdu);
			$this->assign("userWork",$userWork);
			$this->assign("userContact",$userContact);
			$this->assign("userIntro",$userIntro);
			//渲染模版并输出
			$this->display();
		}
		
		
		/**
		  * 迷你博客列表
		  *
		  *
		  **/
		public function mini(){ //迷你博客action
			import("ORG.Util.Page"); //导入分页类
			isset($_GET["u"]) ? $u = safeRequest($_GET["u"]) : $u= "all";    //条件参数
			isset($_GET["p"]) ? $p = $_GET["p"] : $p = 1;       //页数
			if ("all"== $u){ //查找我与朋友所有的迷你博客	
				$mini = D("miniview");
				$friendIds 	= 	getFriendList($this->userInfo['id']);
				$miniCount = $mini->count("mini.userid in ($friendIds)"); //获得相关mini博客记录总数
				$listRows = C("miniListRows"); //从配置文件中读取mini博客每页显示记录数
				$p = new Page($miniCount,$listRows); //实例化分页类
				$p->setConfig('header' ,L("mini_page_title")); 
				$miniList	=	$mini->findAll("mini.userid in ($friendIds)","mini.userid,mini.content,mini.regtime,user.faceimg,user.network",'mini.regtime desc',$p->firstRow.','.$p->listRows);//获得相关迷你博客记录
				$page  = $p->show();
				//$this->assign("userInfo",$this->userInfo); 
				$this->assign("miniCount",$miniCount); 
				$this->assign("miniList",$miniList); //绑定迷你博客数据
				$this->assign("page",$page); //绑定分页
				$this->display("allmini"); //渲染模版输出*/
			} else {//根据用户ID查找相关用户的个人迷你博客
				$this->rank =  getRank($this->userInfo['id'],$u,"mini");
				if(!$this->rank){
					redirect(__APP__."/rank/norank/u/".$u);
					return false;
				}
				$mini = D("mini");
				$miniCount = $mini->count("userid = $u"); //获得相关mini博客记录总数
				$listRows = C("miniListRows"); //从配置文件中读取mini博客每页显示记录数
				$p = new Page($miniCount,$listRows); //实例化分页类
				$p->setConfig('header' ,L("mini_page_title")); 
				$miniList	=	$mini->findAll("userid = $u","*",'id desc',$p->firstRow.','.$p->listRows);//获得相关迷你博客记录
				$page  = $p->show();
				if($u != $this->userInfo['id']){
					$userInfo = D("user")->find("id=".$u,"id,username");
					$this->assign("userInfo",$userInfo); 
				}
				
				$this->assign("userId",$this->userInfo['id']); 
				$this->assign("miniCount",$miniCount); 
				$this->assign("miniList",$miniList); //绑定迷你博客数据
				$this->assign("page",$page); //绑定分页
				$this->display("mymini"); 
			}
		}
		
		/**
		  * 保存迷你博客
		  *
		  *
		  **/
		public function savemini(){//添加迷你博客action
			if("" == trim(htmlspecialchars($_POST["content"],ENT_QUOTES))){
				$this->error(L("no_input_full"));
			}
			//存入数据库
			$mini = D("mini");
			$mini->userid = $this->userInfo['id'];
			$mini->content = htmlspecialchars($_POST["content"],ENT_QUOTES);
			$mini->regtime = time();
			$miniId = $mini->add();
			if($miniId > 0){
				addFeed($this->userInfo['id'],"mini",htmlspecialchars($_POST["content"],ENT_QUOTES),$miniId);
				$path      =  DATA_PATH.$this->userInfo['id']."/";
				$userCache =  D("cache");
				$userCache -> setField("miniCount","(miniCount+1)","userid=".$this->userInfo['id']);
				$userData  =  $userCache->find("userid=".$this->userInfo['id']);
				F("userData",$userData,$path);
				if(isset($_POST["ajax"])){
					echo "success";
				}else{
					$this->redirect("mini/u/".$this->userInfo['id']);
				}
			}else{
				if(isset($_POST["ajax"])){
					echo "error";
				}else{
					$this->error(L("save_error"));
				}
			}
		}
		
		/**
		  * 删除迷你博客
		  *
		  *
		  **/
		public function delmini(){//删除mini博客action
			//数据库删除操作
			$id = safeRequest($_GET["id"]);
			$mini = D("mini");
			if($mini->delete("id=".$id." and userid=".$this->userInfo['id'])){				
				$userCacheDao = D("cache");
				$userCacheDao->setField("miniCount","(miniCount-1)","userid=".$this->userInfo['id']);
				delFeed(0,$this->userInfo['id'],"mini",$id);
				if(isset($_GET["ajax"])){
					echo "success";
				}else{
					$this->redirect("mini/u/".$this->userInfo['id']);
				}
			}else{
				if(isset($_GET["ajax"])){
					echo "error";
				}else{
					$this->error(L("no_record"));
				}			
			}
		}
		
		
		/**
		  * 头像相册
		  *
		  *
		  **/
		public function face(){
			
		}
		
		
		/**
		  * 设置用户头像
		  *
		  *
		  **/
		public function setface(){
			
		}
		
		public function test(){
			$path = getCachePath(APP_PATH."/UserCache",$this->userInfo['id']);
			$userInfo = D("user")->find($this->userInfo['id']);
			F("userInfo",$userInfo,-1,$path);
			$userSetting = D("user_setting")->find($this->userInfo['id']);
			F("userSetting",$userSetting,-1,$path);
			$userData = D("cache")->find($this->userInfo['id']);
			F("userData",$userData,-1,$path);
		}
		
		
	}
?>